package com.newidea.jiangong.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.newidea.jiangong.component.HeatComponent;
import com.newidea.jiangong.entity.HeatFacility;
import com.newidea.jiangong.restful.ApiResult;
import com.newidea.jiangong.service.*;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

@RestController
@Slf4j
@RequestMapping(value = "/api/collect")
public class CollectController {

    private final MachineMonitorService machineMonitorService;
    private final IndoorTemMonitorHeadService indoorTemMonitorHeadService;
    private final HeatFacilityService heatFacilityService;
    private final HeatComponent heatComponent;
    private final HwHeatConvertingService hwHeatConvertingService;
    private final WeatherService weatherService;

    public CollectController(
            MachineMonitorService machineMonitorService,
            IndoorTemMonitorHeadService indoorTemMonitorHeadService,
            HeatFacilityService heatFacilityService,
            HeatComponent heatComponent,
            HwHeatConvertingService hwHeatConvertingService, WeatherService weatherService
    ) {
        this.machineMonitorService = machineMonitorService;
        this.indoorTemMonitorHeadService = indoorTemMonitorHeadService;
        this.heatFacilityService = heatFacilityService;
        this.heatComponent = heatComponent;
        this.hwHeatConvertingService = hwHeatConvertingService;
        this.weatherService = weatherService;
    }

    /**
     * 采集ModbusTCP中的数据
     * <p>
     * 北京市建工锅炉压力容器工程有限公司（大屯）
     * </p>
     */
    @PostMapping("/receiveTcp")
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<ApiResult<Object>> receiveTcp(@RequestBody JSONObject jsonObject) {
        String company = jsonObject.getStr("company");
        log.info("接收到ModbusTCP数据：" + company);
        // 解析ModbusTCP数据
        heatFacilityService.analysis(jsonObject, company);
        log.info("成功采集ModbusTCP数据：" + company);
        return ApiResult.ok();
    }

    /**
     * 采集华威的锅炉数据
     */
    @PostMapping("/hwGl")
    @Transactional(rollbackFor = Exception.class)
    public String receiveHwGl(@RequestBody String text) {
        log.info("接收到华威锅炉数据" );

        JSONArray jsonArray = new JSONArray(text);
        heatFacilityService.hwGlSave(jsonArray);
        log.info("成功采集到华威锅炉数据" );
        return "执行成功!";
    }

    /**
     * 采集华威OPC换热站
     */
    @PostMapping("/reviseHwOpc")
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<ApiResult<Object>> reviseHwOpc(@RequestBody JSONObject jsonObject) {
        // 换热站编号
        String number = jsonObject.getStr("hwOpcNumber");
        log.info("接收到华威换热站数据，编号：" + number);
        // 解析华威OPC换热站
        hwHeatConvertingService.analysisHwOpc(jsonObject, number);
        log.info("成功华威换热站数据，编号：" + number);
        return ApiResult.ok();
    }

    /**
     * 采集机房监控数据
     */
    @PostMapping("/machineMonitor")
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<ApiResult<Object>> machineMonitor(
            @RequestParam String company,
            @RequestParam String account,
            @RequestParam String password
    ) {
        // 获取token
        String token = heatComponent.getToken(account, password);
        machineMonitorService.collect(company, token);
        return ApiResult.ok();
    }


    /**
     * 采集室温监控数据
     */
    @PostMapping("/indoorTemMonitor")
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<ApiResult<Object>> indoorTemMonitor(
            @RequestParam String company,
            @RequestParam String account,
            @RequestParam String password
    ) {
        // 获取token
        String token = heatComponent.getToken(account, password);
        indoorTemMonitorHeadService.collect(company, token);
        return ApiResult.ok();
    }

    @GetMapping("/weather")
    @Operation(summary = "采集天气", description = "采集天气")
    public void weather() {
        weatherService.saveWeather();
    }

}
